#include "TextureSDL_1_3.h"
#include "SystemSDL_1_3.h"

#include <SDL.h>

CTextureSDL_1_3::CTextureSDL_1_3( void ) : m_Format( SDL_PIXELFORMAT_ABGR8888 ),
                                           m_Width( 0 ), m_Height( 0 ), m_hTex( NULL )
{
    //ctor
}

CTextureSDL_1_3::CTextureSDL_1_3( const int width, const int height ) : m_Format( SDL_PIXELFORMAT_ABGR8888 ),
                                                                        m_Width( width ),
                                                                        m_Height( height ),
                                                                        m_hTex( NULL )
{
    //ctor
}

CTextureSDL_1_3::~CTextureSDL_1_3( void )
{
    //dtor
}

int CTextureSDL_1_3::Create( const char* file_name )
{
    int exit_code = 0;

    SDL_assert( CSystemSDL_1_3_Renderer::m_hCurrentRenderer );

    if( m_hTex != NULL )
    {
        SDL_DestroyTexture( m_hTex );
        m_hTex = NULL;
    }

    SDL_Surface* surface = SDL_LoadBMP( file_name );

    if( surface == NULL )
    {
        CSystemHelper::SetError( "Could not load image\n" );
        exit_code = -1;
    }

    m_hTex = SDL_CreateTextureFromSurface( CSystemSDL_1_3_Renderer::m_hCurrentRenderer, surface );

    SDL_FreeSurface( surface );
    surface = NULL;

    if( m_hTex == NULL )
    {
        CSystemSDL_1_3_Helper::SetError( "Could not create texture\n" );
        exit_code = -1;
    }

    return exit_code;
}

void CTextureSDL_1_3::Update( void )
{
    SDL_RenderCopy( CSystemSDL_1_3_Renderer::m_hCurrentRenderer, m_hTex, NULL, NULL );
}

void CTextureSDL_1_3::Destroy( void )
{
    if( m_hTex == NULL )
    {
        SDL_DestroyTexture( m_hTex );
        m_hTex = NULL;
    }
}
